public class LinkedQueue {
    private Node head, tail;
    private int qSize;

    //Constructor
    public LinkedQueue(){
        head=tail=null;
        qSize=0;
    }

    //Accessor methods
    public int size(){
        return qSize;
    }

    public Node getTail(){
        return tail;
    }

    public boolean isEmpty(){
        return(head==null);
    }

    public Object Front()
            throws QueueEmptyException{
        if(isEmpty())
            throw new QueueEmptyException("Queue is Empty");
        else
            return head.getElement();
    }

    //Update methods
    public void append (Object obj){ //insert an element
        Node node = new Node();
        node.setElement(obj);
        node.setNext(null); //node will be new tail node
        if(qSize==0)
            head=node; //special case of a previously empty queue
        else
            tail.setNext(node); //add node at the tail of the list

        tail=node; //update the reference to the tail node
        qSize++;
    }
    
    public Object serve() throws QueueEmptyException{ //Remove the first item from the queue
        Object obj;
        if(qSize==0)
            throw new QueueEmptyException("Queue is empty.");
        else{
            obj=head.getElement();
            head=head.getNext();
            qSize--;
        }
        if(qSize==0)
            tail=null; //Queue is now empty
        return obj;
    }
}